/*
 * jxController Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxController.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JPanel;

import org.jxController.event.CrudEvent;
import org.jxDao.dao.Persistable;
import org.jxSwing.component.frame.InternalFrame;
import org.jxUtils.ReflectionUtil;
import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxController
 * @package org.jxController.view
 * @date 05/05/2014 - 13:21:27
 * @param <Bean>
 */
public abstract class CrudInternalFrame<Bean extends Persistable> extends InternalFrame {
	
	private static final long serialVersionUID = 8571783279658758766L;
	
	private Class<?> beanClass;
	
	protected Bean oldBean = null;
	
	protected Bean bean = null;
	
	private JPanel panel = null;
	
	private CrudBar crudBar = null;
	
	/**
	 * @param owner
	 */
	protected CrudInternalFrame(final JDesktopPane owner) {
	
		this(null, owner);
	}
	
	/**
	 * @param title
	 * @param owner
	 */
	protected CrudInternalFrame(final String title, final JDesktopPane owner) {
	
		super(title, owner);
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		// Obtém as Classes do parâmetro
		final List<Class<?>> parameterClasses = ReflectionUtil.getParameterClass(getClass());
		
		beanClass = parameterClasses.get(0);
		
		getCrudBar().setOwner(getPanel());
		
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		getContentPane().add(getCrudBar(), BorderLayout.SOUTH);
		
		getCrudBar().setPreferredSize(new Dimension(250, 45));
	}
	
	/**
	 * 
	 */
	protected void clearFields() {
	
		getCrudBar().clearFields();
	}
	
	/**
	 * 
	 */
	protected abstract Bean fillBean();
	
	/**
	 * @param bean
	 */
	protected abstract void fillFields(Bean bean);
	
	/**
	 * @param newBean
	 */
	public final void fillFields() {
	
		fillFields(getBean());
	}
	
	/**
	 * 
	 */
	public abstract void firstFocus();
	
	/**
	 * @return the bean
	 */
	public final Bean getBean() {
	
		return bean;
	}
	
	/**
	 * @return the bean in view
	 */
	public final Bean getBeanInView() {
	
		return fillBean();
	}
	
	/**
	 * @return the beanClass
	 */
	public Class<?> getBeanClass() {
	
		return beanClass;
	}
	
	/**
	 * @return the crudBar
	 */
	public final CrudBar getCrudBar() {
	
		if(crudBar == null) {
			crudBar = new CrudBar();
		}
		return crudBar;
	}
	
	/**
	 * @return crudEvent
	 */
	public final CrudEvent getCrudEvent() {
	
		return getCrudBar().getCrudEvent();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JInternalFrame#getFrameIcon()
	 */
	@Override
	public ImageIcon getFrameIcon() {
	
		return ImageUtil.toImageIcon(super.getFrameIcon());
	}
	
	/**
	 * @return a clone of the Bean or null
	 */
	public final Bean getOldBean() {
	
		return oldBean;
	}
	
	/**
	 * @return {@link JPanel}
	 */
	public final JPanel getPanel() {
	
		if(panel == null) {
			panel = new JPanel(null);
			panel.addContainerListener(new ContainerAdapter() {
				
				@Override
				public void componentAdded(final ContainerEvent containerEvent) {
				
					getCrudBar().lockOwnerFields(true);
				}
			});
		}
		return panel;
	}
	
	/**
	 * @return {@code getCrudBar().isShowRefreshButton()}
	 */
	public final boolean isShowRefreshButton() {
	
		return getCrudBar().isShowRefreshButton();
	}
	
	/**
	 * @return {@code getCrudBar().isShowTextInCrudButtons()}
	 */
	public final boolean isShowTextInCrudButtons() {
	
		return getCrudBar().isShowTextInCrudButtons();
	}
	
	/**
	 * 
	 */
	public final void makeOldBean() {

		this.oldBean = ReflectionUtil.clone(this.bean);
	}
	
	/**
	 *
	 */
	public final boolean requiredFieldsAreFilled() {
	
		return getCrudBar().requiredFieldsAreFilled(false);
	}
	
	/**
	 * @param bean
	 */
	public final void setBean(final Bean bean) {

		this.bean = bean;
		
		//
		if(this.bean != null) {
			
			fillFields(this.bean);
			
		}else {
			
			clearFields();
		}
	}
	
	/**
	 * @param crudEvent
	 *        the crudEvent to set
	 */
	public final void setCrudEvent(final CrudEvent crudEvent) {
	
		getCrudBar().setCrudEvent(crudEvent);
	}
	
	/**
	 * @param canFind
	 * @param canInclude
	 * @param canEdit
	 * @param canDelete
	 * @param canPrint
	 */
	public final void setPermissions(final boolean canFind, final boolean canInclude, final boolean canEdit, final boolean canDelete, final boolean canPrint) {
	
		getCrudBar().setPermissions(canFind, canInclude, canEdit, canDelete, canPrint);
	}
	
	/**
	 * @param showRefreshButton
	 * @see #showRefreshButton(boolean)
	 */
	@Deprecated
	public final void setShowRefreshButton(boolean showRefreshButton) {
	
		showRefreshButton(showRefreshButton);
	}
	
	/**
	 * @param showTextInCrudButtons
	 * @see #showTextInCrudButtons(boolean)
	 */
	@Deprecated
	public void setShowTextInCrudButtons(boolean showTextInCrudButtons) {
	
		showTextInCrudButtons(showTextInCrudButtons);
	}
	
	/**
	 * @param showRefreshButton
	 */
	public final void showRefreshButton(boolean showRefreshButton) {
	
		getCrudBar().showRefreshButton(showRefreshButton);
	}
	
	/**
	 * @param showTextInCrudButtons
	 */
	public void showTextInCrudButtons(boolean showTextInCrudButtons) {
	
		getCrudBar().showTextInCrudButtons(showTextInCrudButtons);
	}
}
